package com.centaline.androidsalesblog.widgets;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.BitmapTypeRequest;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.ConsultAdapter;
import com.centaline.androidsalesblog.iml.RecyclerViewItemOnClickListener;
import com.centaline.androidsalesblog.iml.StaffItemDetailOnClick;
import com.centaline.androidsalesblog.model.businissobject.NewPropExtInfoBo;
import com.centaline.androidsalesblog.model.businissobject.PostDetailBo;
import com.centaline.androidsalesblog.model.databaseobject.StaffDo;
import com.centaline.androidsalesblog.utils.PriceUtil;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.glide.RoundImageViewTarget;

import java.util.ArrayList;

/**
 * 咨询自定义
 * Created by guilin on 16/3/9.
 */
public class ConsultView extends LinearLayout implements View.OnClickListener {

    private Context context;
    private LinearLayout ll_root;
    private RelativeLayout rl_staff;
    private ImageView img_staff;
    private AppCompatTextView atv_name, atv_department, atv_im, atv_msg, atv_phone;
    private RecyclerView rv_staffs;

    private BitmapTypeRequest<String> requestBuilder;
    private StaffItemDetailOnClick staffItemDetailOnClick;
    private ArrayList<StaffDo> staffDos = new ArrayList<>();
    private ConsultAdapter consultAdapter;
    private StaffDo currentStaffDo;
    private StringBuilder msgContent = new StringBuilder();
    private StringBuilder imContent = new StringBuilder();

    public ConsultView(Context context) {
        this(context, null);
    }

    public ConsultView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ConsultView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        inflate(context, R.layout.layout_consult, this);
        ll_root = (LinearLayout) findViewById(R.id.ll_root);
        ll_root.setOnClickListener(this);
        rl_staff = (RelativeLayout) findViewById(R.id.rl_staff);
        rl_staff.setOnClickListener(this);
        img_staff = (ImageView) findViewById(R.id.img_staff);
        atv_name = (AppCompatTextView) findViewById(R.id.atv_name);
        atv_department = (AppCompatTextView) findViewById(R.id.atv_department);
        atv_im = (AppCompatTextView) findViewById(R.id.atv_im);
        atv_im.setOnClickListener(this);
        atv_msg = (AppCompatTextView) findViewById(R.id.atv_msg);
        atv_msg.setOnClickListener(this);
        atv_phone = (AppCompatTextView) findViewById(R.id.atv_phone);
        atv_phone.setOnClickListener(this);
        rv_staffs = (RecyclerView) findViewById(R.id.rv_staffs);

        rv_staffs.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        rv_staffs.setHasFixedSize(true);
        consultAdapter = new ConsultAdapter(context, staffDos, new RecyclerViewItemOnClickListener<StaffDo>() {
            @Override
            public void onItemOnClick(StaffDo staffDo, int tag) {
                setCurrentStaff(staffDo);
            }
        });
        rv_staffs.setAdapter(consultAdapter);
    }

    /**
     * 初始化
     */
    public void initRequestBuilder(BitmapTypeRequest<String> requestBuilder,
                                   StaffItemDetailOnClick staffItemDetailOnClick) {
        this.requestBuilder = requestBuilder;
        this.staffItemDetailOnClick = staffItemDetailOnClick;
        consultAdapter.setRequestBuilder(requestBuilder);
    }

    public void setStaffs(ArrayList<StaffDo> staffs) {
        if (staffs == null || staffs.size() == 0) {
            return;
        }
        staffDos.addAll(staffs);
        consultAdapter.notifyDataSetChanged();
        setCurrentStaff(staffDos.get(0));
    }

    public void setPostDetail(PostDetailBo postDetailBo) {
        if (postDetailBo == null)
            return;
        if ("s".equalsIgnoreCase(postDetailBo.getPostDo().getPostType())) {
            msgContent.append("您好，我想咨询关于 ")
                    .append(postDetailBo.getCestName())
                    .append(" ")
                    .append(PriceUtil.salePrice(postDetailBo.getSellPrice()))
                    .append(" ")
                    .append(postDetailBo.getPostDo().getBedroomCount())
                    .append("室")
                    .append(postDetailBo.getPostDo().getSittingRoomCount())
                    .append("厅")
                    .append(" 的情况，如方便请尽快回复，谢谢！");
            imContent.append(postDetailBo.getCestName())
                    .append(" ")
                    .append(postDetailBo.getPostDo().getBedroomCount())
                    .append("室")
                    .append(postDetailBo.getPostDo().getSittingRoomCount())
                    .append("厅")
                    .append(" ")
                    .append(PriceUtil.salePrice(postDetailBo.getSellPrice()))
                    .append("的二手房源");
        } else {
            msgContent.append("您好，我想咨询关于 ")
                    .append(postDetailBo.getCestName())
                    .append(" ")
                    .append(PriceUtil.rentPrice(postDetailBo.getRentalPrice()))
                    .append(" ")
                    .append(postDetailBo.getPostDo().getBedroomCount())
                    .append("室")
                    .append(postDetailBo.getPostDo().getSittingRoomCount())
                    .append("厅")
                    .append(" 的情况，如方便请尽快回复，谢谢！");
            imContent.append(postDetailBo.getCestName())
                    .append(" ")
                    .append(postDetailBo.getPostDo().getBedroomCount())
                    .append("室")
                    .append(postDetailBo.getPostDo().getSittingRoomCount())
                    .append("厅")
                    .append(" ")
                    .append(PriceUtil.rentPrice(postDetailBo.getRentalPrice()))
                    .append("的出租房源");
        }
    }

    /**
     * 新房
     */
    public void setNewEst(NewPropExtInfoBo newPropExtInfoBo) {
        msgContent.append("您好，我想咨询关于 ")
                .append(newPropExtInfoBo.getAdName())
                .append(" 的情况，如方便请尽快回复，谢谢！");
        imContent.append(newPropExtInfoBo.getAdName());
    }

    public void setImEnable(boolean enable) {
        atv_im.setVisibility(enable ? VISIBLE : GONE);
    }

    /**
     * 设置当前的Staff信息
     */
    private void setCurrentStaff(StaffDo staffDo) {
        currentStaffDo = staffDo;
        if (staffDo != null) {
            atv_name.setText(staffDo.getCnName());
            atv_department.setText(staffDo.getDepartmentName());
            if (GlideLoad.needLoad(true)) {
                requestBuilder
                        .load(staffDo.getImgUrl())
                        .placeholder(R.drawable.ic_staff_default)
                        .error(R.drawable.ic_staff_default)
                        .centerCrop()
                        .into(new RoundImageViewTarget(img_staff));
            } else {
                requestBuilder
                        .load("")
                        .placeholder(R.drawable.ic_staff_default)
                        .error(R.drawable.ic_staff_default)
                        .centerCrop()
                        .into(new RoundImageViewTarget(img_staff));
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (currentStaffDo == null)
            return;
        switch (v.getId()) {
            case R.id.rl_staff:
                staffItemDetailOnClick.onClick(currentStaffDo, 0, null, null);
                break;
            case R.id.atv_im:
                staffItemDetailOnClick.onClick(currentStaffDo, 1, imContent.toString(), null);
                break;
            case R.id.atv_msg:
                staffItemDetailOnClick.onClick(currentStaffDo, 2, null, msgContent.toString());
                break;
            case R.id.atv_phone:
                staffItemDetailOnClick.onClick(currentStaffDo, 3, null, null);
                break;
            default:
                break;
        }
    }
}
